<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> 
<meta name="keywords" content="carousel, jquery, responsive, fluid, elastic, resize, thumbnail, slider" />
<meta name="author" content="Codrops" />
<title>微评说客</title>
<link rel="stylesheet" type="text/css" href="/Public/home/style/public.css">
<link rel="stylesheet" type="text/css" href="/Public/home/style/style.css">
<link rel="stylesheet" type="text/css" href="/Public/home/style/font-awesome.css">
<script src="/Public/home/jquery/jquery-1.7.2.min.js"></script>
<script src="/Public/home/jquery/tab.js"></script>
<script type="text/javascript">
var rindex=<eq name="type" value="radio">{$count}<else/>0</eq>;
var cindex=<eq name="type" value="checkbox">{$count}<else/>0</eq>;
$(document).ready(function(e) {
    lr();
});
function lr(){
	$("#button2").click(function(){
		var template='<p id="radio_{{rindex}}"><input name="radio{{rindex}}" type="text" placeholder="填写选择题答案"><a href="javascript:remove1({{rindex}})">删除</a></p>';
		$("#mk3").append(template.replace(/{{rindex}}/g,rindex++));
	});
	$("#button3").click(function(){
		var template='<p id="checkbox_{{cindex}}"><input name="checkbox{{cindex}}" type="text" placeholder="填写选择题答案"><a href="javascript:remove2({{cindex}})">删除</a></p>';
		$("#mk4").append(template.replace(/{{cindex}}/g,cindex++));
	});
}
function remove1(id){
	rindex--;
	$("#radio_"+id).remove();
}	
function remove2(id){
	cindex--;
	$("#checkbox_"+id).remove();
}	

function addradio(){

	var isfilled=checkfill();
	
	if(isfilled){
		data=getdata(1);
		addque(data);
	}
	else{
		alert("请将问题填写完整！");
		
	}
}
function addcheck(){

	var isfilled=checkfill1();
	
	if(isfilled){
		data=getdata(2);
		addque(data);
	}
	else{
		alert("请将问题填写完整！");
		
	}
}
function addtext(){

	var isfilled=checkfill2();
	
	if(isfilled){
		data=getdata(3);
		addque(data);
	}
	else{
		alert("请将问题填写完整！");
		
	}
}
</script>
<style>
	html,body{ background:#f2f2f2;}
</style>
</head>

<body>
<header class="index-header">
	<h1>题目编辑</h1>
    <a href="javascript:window.location.href='post.html?id={$id}';" class="icon-circle-arrow-left kl"></a>
</header>

<section class="table2">
	<div class="tm">
    	<input id="title" type="text" placeholder="题目标题" value="{$info.name}">
    </div>
	<div class="table2-title tab_title">
    	<ul>
    		<li <eq name="type" value="radio">class="current"</eq>>单选题</li>
    		<li <eq name="type" value="checkbox">class="current"</eq>>多选题</li>
    		<li <eq name="type" value="text">class="current"</eq>>问答题</li>
            <div class="clears"></div>
    	</ul>
    </div>
    <div class="table2-nr tab_nr">
    	<div class="bk1 tab_block" <neq name="type" value="radio">style="display:none"</neq>>
        	<div class="bk1-content" id="mk3">
        	<eq name="type" value="radio">
        	<volist name="info.options" id="vo" >
        	<p id="radio_{$i-1}"><input name="radio{$i-1}" type="text" placeholder="填写选择题答案" value="{$vo.text}"> <a href="javascript:remove1({$i-1})">删除</a></p>
        	</volist>
        	</eq>
        	</div>
            <span><input type="button" value="+添加选项" class="be1" id="button2"><input type="button" value="确认" class="be2" onclick="addradio()"></span>
        </div>
    	<div class="bk2 tab_block" <neq name="type" value="checkbox">style="display:none"</neq>>
            <div class="bk1-content" id="mk4">
            <eq name="type" value="checkbox">
        	<volist name="info.options" id="vo" >
        	<p id="checkbox_{$i-1}"><input name="checkbox{$i-1}" type="text" placeholder="填写选择题答案" value="{$vo.text}"><a href="javascript:remove1({$i-1})">删除</a></p>
        	</volist>
        	</eq>
            </div>
            <span><input type="button" value="+添加选项" class="be1" id="button3"><input type="button" value="确认" class="be2" onclick="addcheck()"> </span>
        </div>
    	<div class="bk3 tab_block" <neq name="type" value="text">style="display:none"</neq>>
        	<textarea></textarea>
            <p><input type="button" value="确认" onclick="addtext()"></p>
        </div>
    </div>
</section>
<div class="hh"></div>
<nav>
	<ul>
		<li>
        	<a href="index.html" class="n1">
				<p></p>
				<span>首页</span>
			</a>
        </li>
		<li>
        	<a href="questionnaire.html" class="n2">
				<p></p>
				<span>问卷</span>
			</a>
        </li>
		<li>
        	<a href="Post.html" class="n3">
				<p class="current"></p>
				<span class="current">发布</span>
			</a>
        </li>
		<li>
        	<a href="mine.html" class="n4">
				<p></p>
				<span>我的</span>
			</a>
        </li>
	</ul>
</nav>
</body>
<script type="text/javascript">
function checkfill(){
	var isfilled=true;
	$('input[name^="radio"]').each(function () {
		
		if($(this).val()==""){
			isfilled=false;
		}
	});
	if(!$('input[name^="radio"]').length>0)isfilled=false;
	
	if($('#title').val()=="")isfilled=false;
	return isfilled;
}

function checkfill1(){
	var isfilled=true;
	$('input[name^="checkbox"]').each(function () {
		
		if($(this).val()==""){
			isfilled=false;
		}
	});
	if(!$('input[name^="checkbox"]').length>0)isfilled=false;
	
	if($('#title').val()=="")isfilled=false;
	return isfilled;
}

function checkfill2(){
	var isfilled=true;
	if($('#title').val()=="")isfilled=false;
	return isfilled;
}

function getdata(type){
	var str="";
	if(type==1){
		var i=0;
		$('input[name^="radio"]').each(function () {
			
			str+='\"'+i+'\":{\"type\":\"radio\",\"text\":\"'+$(this).val()+'\"},';i++;
		});
	}
	else if(type==2){
		var j=0;
		$('input[name^="checkbox"]').each(function () {
			
			str+='\"'+j+'\":{\"type\":\"checkbox\",\"text\":\"'+$(this).val()+'\"},';i++;
		});
		
	}
	else{
		str='"0":{"type":"text","text":""},';
	}
	str=str.substring(0,str.length-1);
	return "{"+str+"}";
}


function ToUnicode(str) 
{
 return escape(str).replace(/%/g,"\\").toLowerCase();
}
function addque(data){
	 $.post("/home/index/addquestion",
			  {
		 		name:$("#title").val(),
		 		options:data,
		 		qid:'{$id}'
			  },
			  function(data){
				  if(data.status==0)
			  		 alert(data.id);
				  else{
					  
					  window.location.href='post.html?qid={$id}';
			 		 }
			  });
	
}
</script>
</html>
